/* eslint-disable prefer-const */
import axios from "axios";
import router from "../router";
import { Message } from "element-ui";
// import Vue from 'vue'
// eslint-disable-next-line no-unused-vars

const baseURL = {
  TEST: "http://20211129Mock.com/",
  production: "http://101.42.233.226:8060",
  // production: "http://192.168.43.156:8060", // 热点
  // production: 'http://192.168.235.80' // 志恒热点
  // production: "http://192.168.43.15:8060", //wsj
};
const instance = axios.create({
  baseURL: baseURL.production,
  // timeout: 10000
});
// const test = null
instance.interceptors.request.use(
  function (config) {
    if (router.history.current.fullPath === "/login") {
      return config;
    } else {
      const role = JSON.parse(sessionStorage.getItem("userInfo"));
      // console.log(role)
      config.headers.token = role.token;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  function (response) {
    if (response.data.code === 401) {
      Message({
        showClose: true,
        message: response.data.message + "请重新登录",
        type: "error",
      });
      router.push("/login");
    } else {
      // 对响应数据做点什么
      return response;
    }
  },
  function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);
// HTTP = {
//   install: function (Vue) {
//     Vue.prototype.$http = instance
//   },
//   instance
// }
// Vue.prototype.$http = instance

export default instance;
